<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter, useRoute } from 'vue-router'
import { customsEdit, customsDetail } from '@/api/common'
import { customsClearanceCompanyType } from '@/api/common/types'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { query } = useRoute()

const { t } = useI18n()

const currentRow = ref<Nullable<customsClearanceCompanyType>>(null)

const getTableDet = async () => {
  const res = await customsDetail(query.id as string)
  if (res) {
    console.log('---获取row内容---')
    currentRow.value = res.data
  }
}

getTableDet()

const addWriteRef = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWriteRef)
  console.log('---addWrite---', write?.getFormData())
  const data = {
    id: write?.getFormData()?.id,
    name: write?.getFormData()?.name,
    contact: write?.getFormData()?.contact,
    mobile: write?.getFormData()?.mobile,
    address: write?.getFormData()?.address,
    fax: write?.getFormData()?.fax,
    credit: write?.getFormData()?.credit,
    email: write?.getFormData()?.email,
    bank_account: write?.getFormData()?.bank_account,
    remark: write?.getFormData()?.remark,
    wayArr: write?.getFormData()?.wayArr
  } as customsClearanceCompanyType
  console.log('---客户管理单新增参数---', data)
  const res = await customsEdit(write?.getFormData()?.id, data)
  if (res) {
    emitter.emit('getList', 'edit')
    push('/sysParameter/CustomsClearanceFormCompany')
  }
}
</script>

<template>
  <ContentDetailWrap
    :title="t('exampleDemo.edit')"
    @back="push('/sysParameter/CustomsClearanceFormCompany')"
  >
    <!-- <Write ref="addWriteRef" :current-row="currentRow" /> -->
    <AddWrite ref="addWriteRef" :current-row="currentRow" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
